package com.project.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.server.exception.BusinessException;
import com.project.server.mapper.AdminUserMapper;
import com.project.server.model.AdminUser;
import com.project.server.service.AdminUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
@RequiredArgsConstructor
public class AdminUserServiceImpl implements AdminUserService {

    private final AdminUserMapper adminUserMapper;
    private final PasswordEncoder passwordEncoder;
    
    private static final String DEFAULT_PASSWORD = "123456";

    @Override
    public AdminUser getByUsername(String username) {
        return adminUserMapper.selectOne(
                new LambdaQueryWrapper<AdminUser>()
                        .eq(AdminUser::getUsername, username)
        );
    }

    @Override
    public Page<AdminUser> page(Integer pageNum, Integer pageSize, String username) {
        Page<AdminUser> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<AdminUser> wrapper = new LambdaQueryWrapper<>();
        
        if (StringUtils.hasText(username)) {
            wrapper.like(AdminUser::getUsername, username);
        }
        
        wrapper.orderByDesc(AdminUser::getCreateTime);
        return adminUserMapper.selectPage(page, wrapper);
    }

    @Override
    public void save(AdminUser adminUser) {
        // 检查用户名是否已存在
        if (getByUsername(adminUser.getUsername()) != null) {
            throw new BusinessException("用户名已存在");
        }
        
        // 设置默认密码
        adminUser.setPassword(passwordEncoder.encode(DEFAULT_PASSWORD));
        adminUser.setStatus(1); // 默认启用
        
        adminUserMapper.insert(adminUser);
    }

    @Override
    public void update(AdminUser adminUser) {
        AdminUser existingUser = adminUserMapper.selectById(adminUser.getId());
        if (existingUser == null) {
            throw new BusinessException("管理员不存在");
        }
        
        // 如果修改了用户名，需要检查是否已存在
        if (!existingUser.getUsername().equals(adminUser.getUsername())) {
            if (getByUsername(adminUser.getUsername()) != null) {
                throw new BusinessException("用户名已存在");
            }
        }
        
        // 保持原密码不变
        adminUser.setPassword(existingUser.getPassword());
        
        adminUserMapper.updateById(adminUser);
    }

    @Override
    public void removeById(Long id) {
        adminUserMapper.deleteById(id);
    }

    @Override
    public void resetPassword(Long id) {
        AdminUser adminUser = new AdminUser();
        adminUser.setId(id);
        adminUser.setPassword(passwordEncoder.encode(DEFAULT_PASSWORD));
        adminUserMapper.updateById(adminUser);
    }

    @Override
    public void updatePassword(Long id, String oldPassword, String newPassword) {
        AdminUser adminUser = adminUserMapper.selectById(id);
        if (adminUser == null) {
            throw new BusinessException("管理员不存在");
        }
        
        // 验证旧密码
        if (!passwordEncoder.matches(oldPassword, adminUser.getPassword())) {
            throw new BusinessException("原密码错误");
        }
        
        // 更新密码
        adminUser.setPassword(passwordEncoder.encode(newPassword));
        adminUserMapper.updateById(adminUser);
    }
} 